package com.unina.dropplayer.download;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.Toast;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.DropboxAPI.DropboxFileInfo;
import com.dropbox.client2.ProgressListener;
import com.dropbox.client2.exception.DropboxException;
import com.unina.dropplayer.common.DropPlayerConstants;
import com.unina.dropplayer.utilities.RootFolderBuilder;

public class DownloadTask extends AsyncTask<String, Void, Boolean> {

	private Context mContext;
	private DropboxAPI<?> mApi;
	private String dropBoxFilepath;
	private Toast toast;
	private ProgressListener progressListener;
	private ProgressDialog progressDialog;
	private File downloadedFile;
	FileOutputStream outputStream;
	File file;

	public DownloadTask(Context mContext, DropboxAPI<?> mApi,
			String dropBoxFilepath, Toast toast,
			ProgressListener progressListener, ProgressDialog progressDialog) {
		super();
		this.mContext = mContext;
		this.mApi = mApi;
		this.dropBoxFilepath = dropBoxFilepath;
		this.toast = toast;
		this.progressListener = progressListener;
		this.progressDialog = progressDialog;

	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progressDialog.show();
	}
	
	@Override
	protected Boolean doInBackground(String... paths) {
		File directory = new File(paths[0]);
		file = new File(paths[0], paths[1]);
		DropboxFileInfo dropboxFileInfo = null;
		outputStream = null;
		try {

			directory.mkdir();
			file.createNewFile();

			outputStream = new FileOutputStream(file);
			RootFolderBuilder.createRootFolder(mApi);
			dropboxFileInfo = mApi.getFile(dropBoxFilepath, null, outputStream,
					progressListener);
			this.downloadedFile = file;

			return true;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DropboxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return false;
	}

	@Override
	protected void onCancelled() {
		// TODO Auto-generated method stub
		super.onCancelled();
		if (file != null) {
			file.delete();
		}
		
		if (outputStream != null) {
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void onPostExecute(Boolean result) {
		super.onPostExecute(result);
		progressDialog.dismiss();

		mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
				.parse("file://" + Environment.getExternalStorageDirectory())));
		
		if (result)
			toast = Toast.makeText(mContext, downloadedFile.getName()
					+ DropPlayerConstants.MESSAGE_DOWNLOAD_COMPLETE,
					Toast.LENGTH_SHORT);
		else
			toast = Toast.makeText(mContext, downloadedFile.getName()
					+ DropPlayerConstants.MESSAGE_DOWNLOAD_NOT_COMPLETE,
					Toast.LENGTH_SHORT);
		toast.show();



		toast.show();
	}

}
